using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Deepcode.Flux.Core.Data;

namespace Deepcode.Flux.Core.Systems.Workflow.Bases
{
	/// <summary>
	/// Manager class for the workflow definitions
	/// </summary>
	public class Definitions : BaseManager
	{
		#region Constructors
		public Definitions() : base() { }
		public Definitions(SqlConnection CurrentConnection) : base(CurrentConnection) { }
		public Definitions(SqlConnection CurrentConnection, SqlTransaction CurrentTransaction) : base(CurrentConnection, CurrentTransaction) { }
		#endregion
		#region Data Retrieval
		/// <summary>
		/// Get all workflow definitions on the system for a particular handler
		/// </summary>
		/// <param name="HandlerCodeID"></param>
		/// <returns></returns>
		public Definition[] GetDefinitionsByHandlerID(string HandlerCodeID)
		{
			DataTable results = new DataTable();
			
			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("coreWFDefinitionsGetByHandler");
			da.SelectCommand.Parameters.Add(SQLUtility.ParameterIn("@HandlerCode", SqlDbType.NVarChar, 50, HandlerCodeID));
			da.Fill(results);
			ReleaseConnection();

			List<Definition> list = new List<Definition>();
			foreach (DataRow r in results.Rows)
				list.Add(new Definition(r));

			return list.ToArray();
		}
		/// <summary>
		/// Gets a single definition by ID
		/// </summary>
		/// <param name="ID"></param>
		/// <returns></returns>
		public Definition GetByID(int ID)
		{
			DataTable result = new DataTable();

			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("coreWFDefinitionsGetByID");
			da.SelectCommand.Parameters.Add(SQLUtility.ParameterIn("@id", SqlDbType.Int, 4, ID));
			da.Fill(result);
			ReleaseConnection();

			if (result == null) return null;
			if (result.Rows.Count < 1) return null;

			return new Definition(result.Rows[0]);
		}
		#endregion
		#region Save Record
		/// <summary>
		/// Saves the specified definition to the database
		/// </summary>
		/// <param name="def"></param>
		/// <returns></returns>
		public int Save(Definition def)
		{
			EstablishConnection();
			SqlCommand cmd = this.CreateSqlCommand("coreWFDefinitionsSave");
			SqlParameter result = SQLUtility.ParameterInOut("@id", SqlDbType.Int, 4, def.PK_ID);
			cmd.Parameters.Add(result);

			cmd.Parameters.Add(SQLUtility.ParameterIn("@HandlerCode", SqlDbType.NVarChar, 50, def.HandlerCodeID));
			cmd.Parameters.Add(SQLUtility.ParameterIn("@DefName", SqlDbType.NVarChar, 500, def.DefName));
			cmd.ExecuteNonQuery();
			ReleaseConnection();

			if( def.PK_ID == -1 )
				def.PK_ID = (int) result.Value;

			return def.PK_ID;
		}
		#endregion
		#region Deletion
		/// <summary>
		/// Deletes the specified definition from the database 
		/// </summary>
		/// <param name="ID"></param>
		/// <returns></returns>
		public bool DeleteByID(int ID)
		{
			EstablishConnection();
			SqlCommand cmd = this.CreateSqlCommand("coreWFDefinitionsDeleteByID");
			cmd.Parameters.Add(SQLUtility.ParameterIn("@id", SqlDbType.Int, 4, ID));
			cmd.ExecuteNonQuery();
			ReleaseConnection();

			return true;
		}
		#endregion
	}
}
